import numpy as np

from bpnn import BPNN
from utils import *

train_file = './mnist_train.csv'
test_file = './mnist_test.csv'

train = load_dataset(train_file)
test = load_dataset(test_file)
print('Dataset has been loaded.')

# 输入层、隐藏层和输出层的节点数
input_nodes = 784
hidden_nodes = 300
output_nodes = 10

# 学习率设置为0.3
learning_rate = 0.3

# 迭代次数设为1
epoch = 1

nn = BPNN(input_nodes, hidden_nodes, output_nodes, learning_rate)

for j in range(epoch):
    for i in range(60000):
        train_values = train[i]
        x_train, y_train = split_data(train_values)
        nn.train(x_train, y_train)
    print(j, 'Epoch finish')

cnt = 0
for i in range(10000):
    test_values = test[i]
    x_test, y_test = split_data(test_values)
    y_pred = nn.query(x_test)
    if np.argmax(y_pred) == np.argmax(y_test):
        cnt += 1

print('Accuracy:', cnt / 10000.)
